#include "../process.h"
#include "../disk.h"
/* Implements simulator.h to remove dependency on simulator.c*/
#include "../simulator.h"

#include <stdio.h>
#include <stdlib.h>

int clock;

int main(int argc, char **argv)
{
	struct process *p;
	char *name;
	const int q = 20;

	disk_init();

	for(clock = 0; clock<200; clock++)
	{

		if(clock%q==0)
		{
			name = calloc(sizeof(char), 20);
			snprintf(name, 20, "#%d", clock/q);

			p = proc_create(name, 0, 1+DISK_BLOCK_SIZE*clock/q);

			printf("spawn@   \t%d\"\t", sim_get_clock());
			proc_print(p);

			disk_accept_job(p);
		}

		if(!disk_done() && disk_time_to_event()<=0)
		{

			p = disk_return_job();

			if(proc_done(p))
			{
				printf("return/exit@\t%d\"\t",
					sim_get_clock());
				proc_print(p);
				proc_exit(p);
			}
			else
			{
				printf("return/reIO@\t%d\"\t",
					sim_get_clock());
				proc_print(p);

				disk_accept_job(p);
			}
		}
	}

	return 1;
}

int sim_get_clock()
{
	return clock;
}
